//
// Created by denglibin on 2021/4/9.
//
#include "window.h"
#include <stdlib.h>


/**
 * 初始化操作
 */
static void initSDL(){
    //初始化，成功返回0
    if (SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Can not init video %s\n", SDL_GetError());
        exit(1);
    }
}
/**
 * 创建一个窗口对象
 * @param width 窗口宽度
 * @param height 窗口高度
 * @param title 窗口标题
 * @return
 */
GameWindow * GW_createGameWindow( int width,  int height, const char* title){
    initSDL();
    GameWindow  * window = (GameWindow*)calloc(1, sizeof(GameWindow));
    SDL_Window * sdlWin = SDL_CreateWindow("Hello World",
                           SDL_WINDOWPOS_CENTERED,
                           SDL_WINDOWPOS_CENTERED,
                           width,
                           height,
                                           SDL_WINDOW_SHOWN
    );
    if (sdlWin == NULL) {
        SDL_Log("Can not create window %s\n", SDL_GetError());
        exit(1);
    }

    SDL_Renderer * winRender = SDL_CreateRenderer(sdlWin, -1, SDL_RENDERER_ACCELERATED);
    //设置混合模式，支持alpha透明
    SDL_SetRenderDrawBlendMode(winRender, SDL_BLENDMODE_BLEND);
    window->width = width;
    window->height = height;
    window->sdl_win = sdlWin;
    window->sdl_win_renderer = winRender;
    return window;
}


/**
 * 释放资源
 * @param gameWindow
 */
void GW_freeGameWindow(GameWindow* gameWindow){
    SDL_DestroyRenderer(gameWindow->sdl_win_renderer);
    SDL_DestroyWindow(gameWindow->sdl_win);
    SDL_Quit();
    free(gameWindow);
}